home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
VISUALBA
/
BS2FR202.ZIP
/
BAS2FOR.DOC
< prev
next >
Wrap
Text File
|
1994-01-30
|
13KB
|
344 lines
BAS2FOR.DOC by: Ira F. Kavaler - July, 1992
Version 2.02 - 1/30/94
The BASIC to FORTRAN Language Translator.
-----------------------------------------
This program takes a BASIC program written under BASICA or by one
of my other programs, PROGRAM, and translates it into FORTRAN.
During the 1960's and 1970's my favorite computer language was
FORTRAN IV. When I got my second home computer, a TRS-80 Model I, I
had to concentrate on writing my programs in BASIC. BASIC is a great
language; it is far more powerful than FORTRAN, then and now.
I love writing utilities. One day I wanted to see how difficult
it would be to write a language translator, so I wrote BAS2FOR. It is
by no means complete nor perfectly accurate. That's my fault! I have
so many interests that I roam from problem to problem, usually I switch
developing solutions to these problems when I get tired of the last.
In time I do get back and clean up the mess I started. So, don't be
discouraged if you find one of the many opened ends in this program.
When you see an updated version it means I got back to it and cleaned-
up some.
BASIC is the Child of FORTRAN.
------------------------------
Although the boys at Dartmouth will disagree, BASIC is an
outgrowth of FORTRAN. Once you learn both languages you will also
agree. In fact, if you learn ALGOL (another ancient scientific
computer language) you will discover it is also very similar to
FORTRAN.
The Process.
------------
In order to translate a BASIC program into FORTRAN a few steps
must be taken:
1. The BASIC program must be saved in the ASCII format.
2. The ASCII formatted BASIC program file is converted so that it
does not contain any multi-instruction lines. (This is a form of
Structured BASIC, but it does not have the indenting
characteristic of true Structured BASIC .)
3. The Structured BASIC program file is then translated into FORTRAN
program file, which will be compatible with MicroSoft FORTRAN
Compiler.
BASIC program files that are saved as non-ASCII files (using the
usual SAVE "filename" command are called "tokenized" or "compressed"
files. The term "compressed" should not be confused with compression
techniques used by other compression programs such as PKZIP by PKWARE,
etc.
BASIC has its own special compression scheme that reduces the
number of bytes required to store a BASIC program, not only on tape or
disk, but also in memory! Most BASIC commands are stores as one byte
rather than one byte per letter of the command. Some more advanced
commands and certain line number references are stores as two bytes,
again rather than one byte per digit. In fact, the EDIT, LIST and
LLIST commands are probably the only routines requiring that these
tokens be expanded into ASCII characters.
I have included a number of sample program files that I wrote as
separate teaching and demonstration examples that can be translated:
1. WALLPAPR.BAS is a standard "tokenized" BASIC program file that
calculates the amount of wallpaper needed to cover the walls of a
normal rectanglar shaped room.
2. RECHKBK.BAS is a standard "tokenized" BASIC program file that
performs the same steps required to reconcile a check book against
the monthly statement.
3. PRIME.BAS is a standard "tokenized" BASIC program file that can do
two things; first, if you give it a positive number it will
determine if that number is prime, or if you give it a negative
number it will display all prime number between 1 and that
absolute value of that number. (A prime number is an positive
integer that is only evenly divisible by one and itself.)
Here's what you do.
-------------------
Let's say we want to translate WALLPAPR into FORTRAN. The
tokenized program filename is WALLPAPR.BAS
1. Save the BASIC file in ASCII format:
(TXT is the customary file extension used for ASCII files.)
BASICA Load your BASIC interpreter.
LOAD "WALLPAPR" Load the program file.
SAVE "WALLPAPR.TXT",A Save the ASCII formatted file.
SYSTEM Exit your BASIC interpreter.
2. Translate the ASCII formatted BASIC file to FORTRAN:
(FOR is the customary file extension used for FORTRAN files.)
BAS2FOR Start the BASIC to FORTRAN translator.
WALLPAPR.TXT Give the filename of the ASCII formatted
program file.program
{ENTER} Specify the same path for the
intermediate Structured BASIC file.
y Choose yes to print the Structured BASIC
file along side the translated file.
{ENTER} Specify the same path for the file
FORTRAN file.
You can now use a FORTRAN compiler, such as MicroSoft's, to
compile the FORTRAN source code into machine language object code.
Here's a sample of what to expect.
----------------------------------
The source BASIC program is called "wallpapr" (WALLPAPR.BAS). It
calculates the number of single rolls of wallpaper required for a
rectangular shaped room. The following is the original program as it
was saved in ASCII as WALLPAPR.TXT:
10 ' WALLPAPR.BAS - 1/25/94
15 ' --- Calculate the number of single rolls of wallpaper needed
17 ' for a rectangular room of known dimensions.
20 INPUT "Room width (ft.) ";W: INPUT "Room length (ft.) ";L
40 INPUT "Ceiling height (ft.) ";H: P=2*L+2*W: A=P*H: N=A/30
70 INPUT "Number of windows";NW: INPUT "Number of doors";ND
90 OP=NW+ND: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INR(SR)+1
110 PRINT: PRINT SR;"single rolls": PRINT "without the ceiling."
130 A=A+L*W: N=A/30: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INT(SR)+1
140 PRINT: PRINT SR;"single rolls": PRINT "including the ceiling.": END
The above program contains many multi-instruction lines. The
Structured BASIC file, as it was converted, is listed below, with the
multi-instruction lines replaced by single instruction lines:
10 ' WALLPAPR.BAS - 1/25/94
15 ' --- Calculate the number of single rolls of wallpaper needed
17 ' for a rectangular room of known dimensions.
20 INPUT "Room width (ft.) ";W
INPUT "Room length (ft.) ";L
40 INPUT "Ceiling height (ft.) ";H
P=2*L+2*W
A=P*H
N=A/30
70 INPUT "Number of windows";NW
INPUT "Number of doors";ND
90 OP=NW+ND
SR=N-OP/2
IF INT(SR)<>SR THEN SR=INR(SR)+1
110 PRINT
PRINT SR;"single rolls"
PRINT "without the ceiling."
130 A=A+L*W
N=A/30
SR=N-OP/2
IF INT(SR)<>SR THEN SR=INT(SR)+1
140 PRINT
PRINT SR;"single rolls"
PRINT "including the ceiling."
END
The output file translated to FORTRAN is shown below. Where
necessary BAS2FOR inserts any additional instructions needed to support
the translation:
C WALLPAPR.BAS - 1/25/94
10 CONTINUE
C --- Calculate the number of single rolls of wallpaper needed
15 CONTINUE
C for a rectangular room of known dimensions.
17 CONTINUE
20 WRITE (6,5)
5 FORMAT (1X,17HRoom width (ft.) )
READ (5,25) W
25 FORMAT (F16.6)
WRITE (6,45)
45 FORMAT (1X,18HRoom length (ft.) )
READ (5,35) L
35 FORMAT (I10)
40 WRITE (6,65)
65 FORMAT (1X,21HCeiling height (ft.) )
READ (5,55) H
55 FORMAT (F16.6)
P=2*L+2*W
A=P*H
N=A/30
70 WRITE (6,85)
85 FORMAT (1X,17HNumber of windows)
READ (5,75) NW
75 FORMAT (I10)
WRITE (6,105)
105 FORMAT (1X,15HNumber of doors)
READ (5,95) ND
95 FORMAT (I10)
90 OP=NW+ND
SR=N-OP/2
SR=INR(SR)+1
IF (.NOT.(AINT(SR).NE.SR)) GO TO 50
SR=INR(SR)+1
50 CONTINUE
110 WRITE (6,115)
115 FORMAT (1X)
WRITE (6,125) SR
125 FORMAT (1X,F16.6,12Hsingle rolls)
WRITE (6,135)
1